function SeatReservation(name) {
    let self = this;
    self.name = name;

}
function goBack() {
    window.location.href = "/main";
}

function ReservationsViewModel() {
    let self = this;
    // Editable data
    self.seats = ko.observableArray([

    ]);
    self.addSeat = function() {
        self.seats.push(new SeatReservation(""));
    };
    self.removeSeat = function(seat) {
        self.seats.remove(seat)
    };

    // 获取数据的逻辑
    self.submitData = function () {
        let xhrGetData = new XMLHttpRequest();
        xhrGetData.open('GET', '/getType', true);
        xhrGetData.onreadystatechange = function () {
            if (xhrGetData.readyState === 4) {
                if (xhrGetData.status === 200) {
                    let data = JSON.parse(xhrGetData.responseText);
                    // 将数据插入表格
                    ko.utils.arrayPushAll(self.seats, data);
                } else {
                    console.error('获取数据失败:', xhrGetData.status);
                }
            }
        };
        xhrGetData.send();
    };

    // 提交数据的逻辑
    self.submitDataToBackend = function () {
        let xhrSubmitData = new XMLHttpRequest();
        xhrSubmitData.open('POST', '/submitType', true);
        xhrSubmitData.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
        xhrSubmitData.onreadystatechange = function () {
            if (xhrSubmitData.readyState === 4) {
                if (xhrSubmitData.status === 200) {
                    console.log('数据提交成功:', xhrSubmitData.responseText);
                } else {
                    console.error('数据提交失败:', xhrSubmitData.status);
                }
            }
        };
        xhrSubmitData.send(ko.toJSON(self.seats()));
    };
}

// 创建 ViewModel 并应用绑定
let viewModel = new ReservationsViewModel();
ko.applyBindings(viewModel);
// 在页面加载完成后执行获取数据的方法
document.addEventListener("DOMContentLoaded", function () {
    viewModel.submitData();
});